﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FrmLogin
{
    public partial class Form1 : Form
    {
        clsFunciones _metodos = new clsFunciones();
        DataTable _dtAcceso = new DataTable();

        public Form1()
        {
            InitializeComponent();
        }

        private void OK_Click(object sender, EventArgs e)
        {
            if (!Validar())
            {
                return;
            }

            _dtAcceso = _metodos.GetDataByLogin(this.UsernameTextBox.Text.Replace("'", ""), _metodos.SHA1(this.PasswordTextBox.Text));
            if (_dtAcceso.Rows.Count == 0) MessageBox.Show("Usuario y/o Contraseña incorrecto o no activo","Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
            else 
            {
              foreach (DataRow row in _dtAcceso.Rows)
               {
                  GlobalVars.IdUsuario = Convert.ToInt32(row[0].ToString());
                  GlobalVars.Nombre = row[1].ToString();
                  GlobalVars.Apellido = row[2].ToString();
                  GlobalVars.EsAdmin = Convert.ToBoolean(row[5]);
               }
                DialogResult = System.Windows.Forms.DialogResult.OK;
            }              
        }

        private bool Validar()
        {
            if(UsernameTextBox.Text == "") 
            {
                UsernameTextBox.Focus();
                return false;
            }
            
            if(PasswordTextBox.Text == "") 
            {
                PasswordTextBox.Focus();
                return false;
            }
             return true;
        }

      
    }
}
